package com.example.project.activity.wode;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.example.project.R;
import com.example.project.activity.kaishi.LoginActivity;
import com.example.project.activity.mingxi.MainActivity;
import com.example.project.tool.SharePreferce;

import static android.content.Context.NOTIFICATION_SERVICE;

public class AlarmReceiver extends BroadcastReceiver {
    private NotificationManager m_notificationMgr = null;
    NotificationManager manager ;
    private static final int NOTIFICATION_FLAG = 3;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onReceive(Context context, Intent intent) {
        String text = SharePreferce.getInstance(context).getString("reminder");
        manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        //高版本需要渠道
        if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
            //只在Android O之上需要渠道
            NotificationChannel notificationChannel = new NotificationChannel("myChannelId","channelname"
                    ,NotificationManager.IMPORTANCE_HIGH);
            //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道，通知才能正常弹出
            manager.createNotificationChannel(notificationChannel);
        }
        Intent intent1 = new Intent(context, MainActivity.class);
        //给指定的activity传递通知Id，方便activity消除该通知
        intent1.putExtra("notificationId", 1111);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1, 0);
        Notification notification = new NotificationCompat.Builder(context,"myChannelId")
                .setContentTitle("豚豚记账") // 设置显示的标题
                .setContentText(text) // 消息的详细内容
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.tuntun) // 设置状态栏中的小图片，尺寸一般建议在24×24
                .setContentIntent(pendingIntent) // 关联PendingIntent
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.tuntun))  // 这里也可以设置大图标
                .build();
        manager.notify(1,notification);


//        String remindText = SharePreferce.getInstance(context).getString("remindText");
//        m_notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//        if (intent.getAction().equals(GlobalValues.TIMER_ACTION_REPEATING)) {
//            Log.e("alarm_receiver", "周期闹钟");
//        } else if (intent.getAction().equals(GlobalValues.TIMER_ACTION)) {
//            Log.e("alarm_receiver", "定时闹钟");
//            System.out.println("定时闹钟");
//            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.tuntun);
//            Intent intent1 = new Intent(context, MainActivity.class);
//            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1, 0);
//            Notification notify = new Notification.Builder(context)
//                    .setSmallIcon(R.drawable.tuntun) // 设置状态栏中的小图片，尺寸一般建议在24×24
////                    .setLargeIcon(bitmap) // 这里也可以设置大图标
//                    .setTicker("你该记账了") // 设置显示的提示文字
//                    .setContentTitle("豚豚记账") // 设置显示的标题
//                    .setContentText("您有记账提醒哦") // 消息的详细内容
//                    .setContentIntent(pendingIntent) // 关联PendingIntent
//                    .setNumber(1) // 在TextView的右方显示的数字，可以在外部定义一个变量，点击累加setNumber(count),这时显示的和
//                    .build(); // 需要注意build()是在API level16及之后增加的，在API11中可以使用getNotificatin()来
//            notify.flags |= Notification.FLAG_AUTO_CANCEL;
//            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//            manager.notify(1, notify);
//            bitmap.recycle(); //回收bitmap
//        }

    }


}
